<?php

/**
 *  @FileComment            [文件注释内容模板]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：CategoryModel.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-9 22:43:50
 *  *
 */
class CategoryModel extends Model {

        public $table = 'category';
        public $validate = array(
            array('type_tid', 'nonull', '请选择分类所属类型', 2, 3),
            array('cname', 'nonull', '分类名不能为空', 2, 3),
            array('sort', 'nonull', '排序不能为空', 2, 3),
            array('cname', 'maxlen:45', '分类名称最多45个字符', 2, 3)
        );

        public function add_data($pid) {
                return $this->add();
        }

        public function tree_data() {
                $data = $this->all();
                return Data::tree($data, 'cname', 'cid', 'pid');
        }

        public function del_data($cid) {
                $data = $this->where(array('pid' => $cid))->find();
                if ($data) {
                        $this->error = '请先删除该分类下的子分类';
                        return false;
                } else {
                        return $this->where(array('cid' => $cid))->del();
                }
        }

        public function get_tree_data($tid) {
                $data = $this->where(array('type_tid' => $tid))->all();
                return Data::tree($data, 'cname', 'cid', 'pid');
        }

        public function get_data($pid) {
                return $this->where(array('cid' => $pid))->find();
        }

        public function get_all_data() {
                return $this->all();
        }

        public function edit_data() {
                $this->update();
        }

        public function get_some_data($cid = NULL) {
                return $this->where(array('cid' => $cid))->all();
        }

        public function get_pid_data($where) {
                return $this->where($where)->all();
        }

        public function get_cate_data() {
                return $this->where(array('pid!=0'))->limit(16)->all();
        }

        public function get_detail_cate() {
                return $this->where('pid!=0 and type_tid=6')->limit(16)->all();
        }

}
